package com.example.workrecord.service;

import com.example.workrecord.dao.AlumniDao;
import com.example.workrecord.dao.BaseJPA;
import com.example.workrecord.entity.Alumni;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.BindException;
import java.util.List;

@Service
public class AlumniService extends BaseService<Alumni,Long> {
    @Autowired
    private AlumniDao dao;
    @Override
    protected BaseJPA<Alumni> getDao() {
        return dao;
    }

    public void deleteByGuestId(Long id) throws Exception{
        List<Alumni> list=dao.findAllByGuestId(id);
        if(null ==list || list.size()==0){
            return;
        }
        for(Alumni alumni:list){
            delete(alumni.getId());
        }
    }

    public void eidit(List<Alumni> alumniList, Long id) throws Exception {
        List<Alumni> list=dao.findAllByGuestId(id);
        if(null == list ){
            saveAll(alumniList);
            return;
        }
        for(Alumni alumni: alumniList){
            if(null == alumni.getId()||alumni.getId().equals("")){
                save(alumni);
                continue;
            }
            for(Alumni alumni1:list){
                if(alumni.getId().equals(alumni1.getId())){
                    list.remove(alumni1);
                }
            }
        }
        if(null != list&& list.size()>0){
            for(Alumni alumni1:list){
                delete(alumni1.getId());
            }
        }
    }

    public List<Alumni> findByGuestId(Long id) {
        return dao.findAllByGuestId(id);
    }
}
